/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package asa;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author scriptoff
 */
class Variable {

    public static final int INT_TYPE = 1;
    public static final int ARRAY_TYPE = 2;
    private int valeur = 0;
    private int type;
    private ArrayList<Variable> list;

    public int getType() {
        return type;
    }

    public int getValeur() {
        return valeur;
    }

    public Integer getValeur(List<Integer> indexList) {
        Integer ret = null;
        Variable var;
        if (type == ARRAY_TYPE && !indexList.isEmpty()) {
            try {
                var = list.get(indexList.get(0));
                indexList.remove(0);
                ret = var.getValeur(indexList);
            } catch (IndexOutOfBoundsException ex) {
                System.out.println("Index out of bound");
                System.exit(1);
            }
        } else if (indexList.isEmpty() && type == INT_TYPE) {
            return valeur;
        } else {
            System.out.println("Index out of bound");
            System.exit(1);
        }
        return ret;
    }

    public Variable(int valeur) {
        type = INT_TYPE;
        this.valeur = valeur;
    }

    public Variable() {
        type = ARRAY_TYPE;
        list = new ArrayList<Variable>();
    }

    public Variable setValeur(List<Integer> indexList, int valeur) {
        if (indexList.isEmpty()) {
            type = INT_TYPE;
            this.valeur = valeur;
        } else {
            Variable v  = null;
            if (list == null) {
                type = ARRAY_TYPE;
                list = new ArrayList<Variable>();
            }
            try {
                int index = indexList.get(0);
                indexList.remove(0);
                try {
                    v = list.get(index);
                } catch (IndexOutOfBoundsException ex) {
                    v = new Variable();
                    list.add(index,v);
                }
                v.setValeur(indexList, valeur);
            } catch (IndexOutOfBoundsException ex) {
                System.out.println("Index out of bound");
                System.exit(1);
            }
        }
        return this;
    }
}
